<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\model;

use think\Model;
use app\api\model\User;

class NewsCollect extends Model
{
    protected $name = 'cms_news_collect';

    // 关联模型
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function news()
    {
        return $this->hasOne(News::class, 'id', 'news_id')->append(['url','content_images','comment_count','collect_count','catalog_title']);
    }

    // 搜索器
    public function searchKeywordAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("news_title", 'like', '%' . $value . '%');
        }
    }

    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }
}